package com.sosee.cgfw.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.EdgeEffectCompat;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.sosee.cgfw.R;
import com.sosee.cgfw.camera.FragmentAdapter;
import com.sosee.cgfw.entity.ad.ADJson;
import com.sosee.cgfw.fragment.SplashAdFragment;
import com.sosee.cgfw.utils.Constans;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Thinkpad on 2016/4/22.
 */
public class SplashAdActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {
    private ViewPager vp;//滑动页
    private FragmentAdapter adapter;
    private List<Fragment> list;//滑动页集合
    private List<ADJson> aList;//广告信息
    private HttpUtils httpUtils;
    //实现 验证码倒计时功能
    private int recLen =6;
    private TextView txtView;
    Timer timer = new Timer();

    final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            switch (msg.what) {
                case 1:
                    if(recLen < 0){
                        timer.cancel();
//                        txtView.setVisibility(View.GONE);
                        recLen=6;
                        startActivity(new Intent(SplashAdActivity.this,MainActivity.class));
                        finish();
                    }else{
                        txtView.setText(""+recLen);
                    }
            }
        }
    };






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad);
        initView();
        timer=new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                recLen--;
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
            }
        };
        //验证码再次发送倒计时
        timer.schedule(task, 1000, 1000);    // timeTask
    }
    private int position=0;


    private EdgeEffectCompat leftEdge,rightEdge;
    public void initView(){
        txtView= (TextView) findViewById(R.id.tv);
        list=new ArrayList<Fragment>();
        aList=new ArrayList<ADJson>();
        vp= (ViewPager) findViewById(R.id.vp);
        adapter=new FragmentAdapter(getSupportFragmentManager());
        adapter.setList(list);
        vp.setAdapter(adapter);
        vp.setOnPageChangeListener(this);
        //用于判断是否滑动至最后一页
        try {
            Field leftEdgeField = vp.getClass().getDeclaredField("mLeftEdge");
            Field rightEdgeField = vp.getClass().getDeclaredField("mRightEdge");
            if (leftEdgeField != null && rightEdgeField != null) {
                leftEdgeField.setAccessible(true);
                rightEdgeField.setAccessible(true);
                leftEdge = (EdgeEffectCompat) leftEdgeField.get(vp);
                rightEdge = (EdgeEffectCompat) rightEdgeField.get(vp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        getAd();
    }

    public void initData(){
        for (int i=0;i<aList.size();i++){
            SplashAdFragment fragment=new SplashAdFragment();
            Bundle bundle=new Bundle();
            String url=Constans.BASE_URL.substring(0, Constans.BASE_URL.length()-1)+aList.get(i).getData().getPic();
            bundle.putString("url",url);
            fragment.setArguments(bundle);
            list.add(fragment);
        }
        adapter.setList(list);
        adapter.notifyDataSetChanged();

    }

    //获取广告信息
    public void getAd(){
        httpUtils=new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.AD_SPLASH_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                aList = JSONArray.parseArray(responseInfo.result, ADJson.class);
                initData();
            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
    }






    private boolean isScorlled=true;

    //处理广告滑动时 事件
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if(isScorlled){
            if(rightEdge!=null&&!rightEdge.isFinished()){//到了最后一张并且还继续拖动，出现蓝色限制边条了
                isScorlled=false;
                startActivity(new Intent(this, MainActivity.class));
                finish();
            }
        }

    }

    @Override
    public void onPageSelected(int position) {
        this.position=position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

//        switch (state) {
//            case ViewPager.SCROLL_STATE_IDLE:
//                if (position==list.size()-1) {
//                    startActivity(new Intent(this, MainActivity.class));
//                    finish();
//                }
//                break;
//        }

    }




}
